package com.glgc.projecti.web.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import com.glgc.projecti.common.CommonUtils;

/**
 * Controller基类，所有的Controller必须要继承此类
 * <p/>
 * 
 * @author <a href="mailto:xcai@isoftstone.com">Alvin Ai</a>
 * @version  Date: Oct 29, 2014 8:50:15 PM
 * @serial 1.0
 * @since Oct 29, 2014 8:50:15 PM
 */
public class BaseController {
	
	public static final String DEFAULT_JSON_DATA = "rows";

	public static final String DEFAULT_JSON_TOTAL_PROPERTY = "total";

	public static final String DEFAULT_JSON_MESSAGE = "message";

	public static final String DEFAULT_JSON_SUCCESS = "success";
	
	public static final String DEFAULT_TOTAL = "totalPage";

	protected  Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * 
	 * 更具异常获取异常信息
	 * <p/>
	 * @author Alvin Ai
	 * @date Oct 29, 2014 8:54:03 PM
	 * @param e
	 * @return
	protected String getMessage(BaseException e) {
		return MsgDescription.getMsgDesc(e.getErrorCode());
	}
	 */

	/**
	 * 
	 * 更具错误码获取错误信息
	 * <p/>
	 * @author Alvin Ai
	 * @date Oct 29, 2014 8:54:24 PM
	 * @param code
	 * @return
	protected String getMessage(int code) {
		return MsgDescription.getMsgDesc(code);
	}
	 */


	protected JSONObject toJSONResult(long count, List<?> data) {
		JSONObject result = new JSONObject();
		result.put(DEFAULT_JSON_TOTAL_PROPERTY, count);
		result.put(DEFAULT_JSON_DATA, data);
		return result;
	}

	protected JSONObject toJSONResult(boolean success, String message) {
		JSONObject result = new JSONObject();
		result.put(DEFAULT_JSON_SUCCESS, success);
		if (!CommonUtils.isEmpty(message)) {
			result.put(DEFAULT_JSON_MESSAGE, message);
		}
		return result;
	}

	protected JSONObject toJSONResult(boolean success) {
		JSONObject result = new JSONObject();
		result.put(DEFAULT_JSON_SUCCESS, success);
		return result;
	}

	protected JSONObject toJSONResult(boolean success, Object data) {
		JSONObject result = new JSONObject();
		result.put(DEFAULT_JSON_SUCCESS, success);
		result.put(DEFAULT_JSON_DATA, data);
		return result;
	}
	
	protected JSONObject toJSONResult(boolean success, Object data, int totalPage) {
		JSONObject result = new JSONObject();
		result.put(DEFAULT_JSON_SUCCESS, success);
		result.put(DEFAULT_JSON_DATA, data);
		result.put(DEFAULT_TOTAL, totalPage);
		return result;
	}

	protected Map<String, Object> toJSONResultMap(boolean success, Object data) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(DEFAULT_JSON_SUCCESS, success);
		result.put(DEFAULT_JSON_DATA, data);
		return result;
	}

	protected Map<String, Object> toJSONResultMap(boolean success, String msg) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(DEFAULT_JSON_SUCCESS, success);
		result.put(DEFAULT_JSON_MESSAGE, msg);
		return result;
	}

	protected JSONArray toJSONArrayResult(List<?> data) {
		return JSONArray.fromObject(data);
	}
	
	protected Pageable convert2Pageable(int start, int limit) {
		if (limit == 0) {
			limit = Integer.MAX_VALUE;
		}
		Pageable pageable = new PageRequest(start / limit, limit);
		return pageable;
	}

	

}
